#include "historywriter.h"
#include "grid.h"

#include <QString>
#include <QFile>
#include <QDateTime>

HistoryWriter::HistoryWriter() : QObject()
{
    randomStimFiles = 0;
    singleStimFiles = 0;
    savePath = QString("C:/");
    currentFile = new QFile("");
}

void HistoryWriter::setSavePath(QString path)
{
    printf("save path changed\n"); fflush(stdout);
    path.replace("\\", "/");
    if (path.endsWith("/"))
    {
        path.chop(1);
    }

    savePath = path;
}

void HistoryWriter::saveHistory(Grid grid, int **squares, int prestimFrames, int stimFrames, int poststimFrames)
{
    printf("hi2\n"); fflush(stdout);
    QDateTime now = QDateTime::currentDateTime();

    QString str = QString("/trialHistory_single_%1_%2.txt").arg(singleStimFiles, 2, 10, QChar('0')).arg(now.toString("hhmmss"));
    currentFile->setFileName(str);

    currentFile->open(QIODevice::WriteOnly);

    const QString gridInfo = QString("Grid Size\t%1 %2\nGeometry\t%3\t%4\t%5\t%6\nGridWidth\t%7\nnActive\t%8\nTiming\t%9\t%10\t%11\n")
            .arg(grid.gridSize[0])
            .arg(grid.gridSize[1])
            .arg(grid.geometry[0])
            .arg(grid.geometry[1])
            .arg(grid.geometry[2])
            .arg(grid.geometry[3])
            .arg(grid.gridWidth)
            .arg("single_stim")
            .arg(prestimFrames)
            .arg(stimFrames)
            .arg(poststimFrames);

    currentFile->write(gridInfo.toStdString().c_str());

    for (int sx = 0; sx  < grid.gridSize[0]; sx++)
    {
        for (int sy = 0; sy < grid.gridSize[1]; sy++)
        {
            currentFile->write(QString("%1\t").arg(squares[sx][sy]).toStdString().c_str());
        }
        currentFile->write("\n");
    }

    singleStimFiles++;
    currentFile->close();
}

void HistoryWriter::saveHistory(Grid grid, const vector<vector<int> > &indices, int nSpots, int prestimFrames, int stimFrames, int poststimFrames)
{
    printf("hi\n");fflush(stdout);
    QDateTime now = QDateTime::currentDateTime();

    QString str = QString("/trialHistory_random_%1_%2.txt").arg(randomStimFiles, 2, 10, QChar('0')).arg(now.toString("hhmmss"));
    currentFile->setFileName(savePath + str);

    currentFile->open(QIODevice::WriteOnly);

    const QString gridInfo = QString("Grid Size\t%1 %2\nGeometry\t%3\t%4\t%5\t%6\nGridWidth\t%7\nnActive\t%8\nTiming\t%9\t%10\t%11\n")
            .arg(grid.gridSize[0])
            .arg(grid.gridSize[1])
            .arg(grid.geometry[0])
            .arg(grid.geometry[1])
            .arg(grid.geometry[2])
            .arg(grid.geometry[3])
            .arg(grid.gridWidth)
            .arg(nSpots)
            .arg(prestimFrames)
            .arg(stimFrames)
            .arg(poststimFrames);

    currentFile->write(gridInfo.toStdString().c_str());

    for (uint trial = 0; trial < indices.size(); trial++)
    {
        for (uint spot = 0; spot < indices[trial].size(); spot++)
        {
            currentFile->write(QString("%1\t").arg(indices[trial][spot]).toStdString().c_str());
        }
        currentFile->write(QString("\n").toStdString().c_str());
    }

    randomStimFiles++;
    currentFile->close();
}
